package com.three.crm.page.procure;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.three.crm.R;
import com.three.crm.adapter.NinePictureAdapter;
import com.three.crm.base.BaseActivity;
import com.three.crm.bean.FileBean;
import com.three.crm.constant.AppConstant;
import com.three.crm.http.CommonResultCallBack;
import com.three.crm.require.AppRequireService;
import com.three.crm.util.AppManager;
import com.three.crm.util.SignUtil;
import com.three.crm.util.TString;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 磅单详情
 * created by lqd on 2024-01-05.
 */
public class PoundDetailActivity extends BaseActivity {

    private static final int REQUEST_GET_DETAIL = 305;
    private int id;
    private TextView tvTitle;
    private EditText etCustomer,etRemark;
    private RecyclerView recyclerView;
    private NinePictureAdapter adapter;

    @Override
    protected void initParms(Bundle bundle) {
        if (bundle != null) {
            id = bundle.getInt(AppConstant.EXTRAS_DATA);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_pound_detail;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText("磅单详情");

        etCustomer = $(R.id.et_customer);
        etRemark = $(R.id.et_remark);

        recyclerView = $(R.id.rv_picture);
        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new NinePictureAdapter();
        adapter.setType("detail");
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
        }
    }

    @Override
    protected void initData(Context mContext) {
        queryDetail();
    }

    private void queryDetail() {
        if (id != 0) {
            BasePopupView popupView = new XPopup.Builder(this)
                    .asLoading("正在加载...").show();
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("userToken", AppManager.getToken());
                jsonObject.put("id",id);
                jsonObject.put("timestamp",getTimestamp());
                String sign = SignUtil.genSign(jsonObject);
                jsonObject.put("sign",sign);
                Log.i("TAG","jsonObject="+jsonObject.toString());
                AppRequireService.queryFormDetail(this, REQUEST_GET_DETAIL, jsonObject, new CommonResultCallBack(this) {
                    @Override
                    public void onResult(String result, int requireCode) {
                        popupView.smartDismiss();
                        Log.i("TAG","result="+result);
                        if (requireCode == REQUEST_GET_DETAIL) {
                            if (!TString.isEmpty(result)) {
                                try {
                                    JSONObject object = new JSONObject(result);
                                    setDetail(object);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }

                    @Override
                    public void onConnectTimeOut(Call call, Exception e, int requireCode) {
                        popupView.smartDismiss();
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onFail(String msg, int requireCode) {
                        popupView.smartDismiss();
                        showToast(msg);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void setDetail(JSONObject object) {
        if (object != null) {
            try {
                JSONObject company = object.getJSONObject("company");
                etCustomer.setText(company.getString("title"));
                etRemark.setText(object.getString("note"));
                JSONArray array = object.getJSONArray("imgs_arr");
                if (array.length() > 0) {
                    List<FileBean> list = new ArrayList<>();
                    for (int i = 0; i < array.length(); i++) {
                        FileBean fileBean = new FileBean();
                        fileBean.setUrl(array.getString(i));
                        list.add(fileBean);
                    }
                    adapter.setNewData(list);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}